/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.entity;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 *
 * @author paolo
 */
@Entity
public class User implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /*ATTRIBUTE*/
    
    @Id
    @Pattern(regexp = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
            message = "invalid email")
    @NotNull(message = "May not be empty")
    private String email;
    
    @NotNull(message = "May not be empty")
    private String password;
    
    private String groupname;
    
    @NotNull(message = "May not be empty")
    private String name;
    
    @NotNull(message = "May not be empty")
    private String surname;
   
    private Boolean publicCalendar;
    
    /*RELATIONSHIP*/
    
    //USER-INVITEDLIST
    @OneToMany(mappedBy = "user")
    private Collection<InvitedList> invitedListCollection;
    
    //USER-EVENT
    @OneToMany(mappedBy = "author")
    private Collection<Event> eventCollection;
    
    //USER-MESSAGELIST
    @OneToMany(mappedBy = "user")
    private Collection<MessageList> messageListCollection;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("SHA-256"); 
            byte[] hash;
            hash = digest.digest(password.getBytes("UTF-8"));
            BigInteger bigInt = new BigInteger(1, hash);
            this.password = bigInt.toString(16);
        } catch (UnsupportedEncodingException |NoSuchAlgorithmException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getGroupname() {
        return groupname;
    }

    public void setGroupname(String groupname) {
        this.groupname = groupname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Boolean getPublicCalendar() {
        return publicCalendar;
    }

    public void setPublicCalendar(Boolean publicCalendar) {
        this.publicCalendar = publicCalendar;
    }

    public Collection<InvitedList> getInvitedListCollection() {
        return invitedListCollection;
    }

    public void setInvitedListCollection(Collection<InvitedList> invitedListCollection) {
        this.invitedListCollection = invitedListCollection;
    }

    public Collection<Event> getEventCollection() {
        return eventCollection;
    }

    public void setEventCollection(Collection<Event> eventCollection) {
        this.eventCollection = eventCollection;
    }

    public Collection<MessageList> getMessageListCollection() {
        return messageListCollection;
    }

    public void setMessageListCollection(Collection<MessageList> messageListCollection) {
        this.messageListCollection = messageListCollection;
    }
}
